/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.entities;

import java.io.Serializable;
//import java.math.;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.hibernate.annotations.Type;

/**
 *
 * @author Samy.A.Omar
 */

@Entity
@Table(name = "USERS")
@NamedQueries({
    @NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
    @NamedQuery(name = "User.findByUserId", query = "SELECT u FROM User u WHERE u.userId = :userId"),
    @NamedQuery(name = "User.findByUserName", query = "SELECT u FROM User u WHERE u.userName = :userName"),
    @NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email"),
    @NamedQuery(name = "User.findByLoginUsername", query = "SELECT u FROM User u WHERE u.loginUsername = :loginUsername"),
    @NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password")})


public class User implements Serializable {
	
    private static final long serialVersionUID = 1L;
    
    
    //@Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    //@Basic(optional = false)
    //@NotNull(message="Employee name must be specified")
    //@Past
    //private Date startDate;
    //@Transient
    
    @Column(name = "USER_ID")
    @SequenceGenerator(name="Users_Gen", sequenceName="USERS_SEQ")
    @Id 
    @GeneratedValue(generator="Users_Gen")
    private Integer userId;   
    
    @Size(max = 50)
    @NotNull(message="USER name must be specified")
    @Column(name = "USER_NAME")
    private String userName;
    
    @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Size(max = 50)
    @Column(name = "EMAIL")
    private String email;
    
    @NotNull(message="LOGIN USER name must be specified")
    @Size(max = 50)
    @Column(name = "LOGIN_USERNAME")
    private String loginUsername;
    
    @Size(max = 50)
    @Column(name = "PASSWORD")
    private String password;
    
    @Column(name = "ACTIVE")
    @Type(type="yes_no")
    private boolean  active ;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID")
    private Role role;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "GROUP_ID", referencedColumnName = "GROUP_ID")
    private Group group;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "COUNTRY_ID", referencedColumnName = "COUNTRY_ID")
    private Country country;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "BRANCH_ID", referencedColumnName = "BRANCH_ID")
    private Branch branch;
    
    @OneToMany(mappedBy = "initiatorUser", fetch = FetchType.LAZY,cascade=CascadeType.PERSIST)
    private Collection<Ticket> initiatedTickets;
    
    @OneToMany(mappedBy = "assignedUser", fetch = FetchType.LAZY, cascade=CascadeType.PERSIST)
    private Collection<Ticket> AssignedTickets;
    

    public User() {
    }

    public User(Integer userId) {
        this.userId = userId;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getLoginUsername() {
        return loginUsername;
    }

    public void setLoginUsername(String loginUsername) {
        this.loginUsername = loginUsername;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    
    
    
    public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public Country getCountry() {
		return country;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public Branch getBranch() {
		return branch;
	}

	public void setBranch(Branch branch) {
		this.branch = branch;
	}

	public Collection<Ticket> getInitiatedTickets() {
		return initiatedTickets;
	}

	public void setInitiatedTickets(Collection<Ticket> initiatedTickets) {
		this.initiatedTickets = initiatedTickets;
	}

	public Collection<Ticket> getAssignedTickets() {
		return AssignedTickets;
	}

	public void setAssignedTickets(Collection<Ticket> assignedTickets) {
		AssignedTickets = assignedTickets;
	}

	@Override
    public int hashCode() {
        int hash = 0;
        hash += (userId != null ? userId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "model.entities.User[ userId=" + userId + " ]";
    }
    
}
